package com.suep.data_analysis.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.suep.data_analysis.dto.DriveSetDto;
import com.suep.data_analysis.dto.DrivePropertyDto;
import com.suep.data_analysis.mapper.DriveSetMapper;
import com.suep.data_analysis.mapper.DrivePropertyMapper;
import com.suep.data_analysis.model.DriveSet;
import com.suep.data_analysis.model.DriveProperty;
import com.suep.data_analysis.repository.DriveSetRepository;
import com.suep.data_analysis.repository.DrivePropertyRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class DriveSearchService {
    private final DriveSetRepository driveSetRepository;
    private final DrivePropertyRepository drivePropertyRepository;
    private final DriveSetMapper driveSetMapper;
    private final DrivePropertyMapper drivePropertyMapper;

    /**
     * 根据套装ID查询详情（含属性）
     */
    public DriveSetDto getDriveSetById(String setId) {
        Objects.requireNonNull(setId, "套装ID不能为null");

        // 构建套装查询条件
        Wrapper<DriveSet> setQuery = Wrappers.<DriveSet>lambdaQuery()
                .eq(DriveSet::getSetId, setId);

        // 查询套装基本信息
        DriveSet driveSet = driveSetRepository.selectDriveSet(setQuery).stream()
                .findFirst()
                .orElse(null);

        if (driveSet == null) {
            return null;
        }

        // 转换为DTO
        DriveSetDto dto = driveSetMapper.toDriveSetDto(driveSet);

        // 查询该套装下的所有属性（无需额外参数，直接通过套装ID关联）
        List<DriveProperty> properties = drivePropertyRepository.selectBySetId(setId);
        dto.setProperties(properties.stream()
                .map(drivePropertyMapper::toDrivePropertyDto)
                .collect(Collectors.toList()));

        return dto;
    }

    /**
     * 查询所有驱动盘套装（不含属性，用于列表展示）
     */
    public List<DriveSetDto> getAllDriveSets() {
        List<DriveSet> driveSets = driveSetRepository.selectAllDriveSets();
        return driveSets.stream()
                .map(driveSetMapper::toDriveSetDto)
                .collect(Collectors.toList());
    }

    /**
     * 根据关键词搜索套装（支持名称、效果搜索）
     */
    public List<DriveSetDto> searchDriveSets(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return getAllDriveSets(); // 空关键词返回全部
        }

        Wrapper<DriveSet> query = Wrappers.<DriveSet>lambdaQuery()
                .like(DriveSet::getSetName, keyword)
                .or()
                .like(DriveSet::getTwoPieceEffect, keyword)
                .or()
                .like(DriveSet::getFourPieceEffect, keyword);

        List<DriveSet> driveSets = driveSetRepository.selectDriveSet(query);
        return driveSets.stream()
                .map(driveSetMapper::toDriveSetDto)
                .collect(Collectors.toList());
    }

    /**
     * 根据位置查询驱动盘属性（用于属性筛选）
     */
    public List<DrivePropertyDto> getPropertiesByPosition(Integer position) {
        Objects.requireNonNull(position, "位置参数不能为null");

        Wrapper<DriveProperty> query = Wrappers.<DriveProperty>lambdaQuery()
                .eq(DriveProperty::getPosition, position);

        List<DriveProperty> properties = drivePropertyRepository.selectDriveProperty(query);
        return properties.stream()
                .map(drivePropertyMapper::toDrivePropertyDto)
                .collect(Collectors.toList());
    }
}